﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AndroidControlePanel : MonoBehaviour
{
    public OprationWidget m_MoveWidget;
    public OprationWidget m_RotationWidget;
    public GameObject m_LockB;
    public Button m_FireButton;

    public Control m_Controle;

    bool m_AutoFire = false;
    void Awake()
    {
        m_MoveWidget.OnDirectionChangedCallback = ShipMove;
        m_RotationWidget.OnDirectionChangedCallback = ShipRot;

            //m_RotationWidget.OnDistanceMaxCallback = Fire;
    }
    private void Start()
    {
        OnOperationModeChanged();
    }

    // Update is called once per frame
    void Update()
    {
        if(m_AutoFire)
        {
            Fire();
        }
    }
    public void SetAutoFire(bool autoFire)
    {
        m_AutoFire = autoFire;
    }

    public Control.ShipOprate ShipMove(Vector3 direction, int source)
    {
        if(m_Controle.m_IsFreeCamera)
        {
            m_Controle.MoveCamera(direction);
            return Control.ShipOprate.MOVE_UP;
        }
        if (Player.Instance.m_EasyOperation)
        {
            return m_Controle.ShipEasyMove(direction, source);
        }
        else
        {
            return m_Controle.ShipMove(direction, source);
        }
    }
    public Control.ShipOprate ShipRot(Vector3 direction, int source)
    {
        return m_Controle.ShipFoucs(direction, source);
    }

    public void Fire()
    {
        m_Controle.Fire(1);
    }
    public void FireUp()
    {
        m_Controle.FireUp(1);
    }

    public void LockMove()
    {
        m_Controle.m_LockMove = true;
    }
    public void UnLockMove()
    {
        m_Controle.m_LockMove = false;
    }

    public void SwitchOperationMode()
    {
        Player.Instance.m_EasyOperation = !Player.Instance.m_EasyOperation;
        OnOperationModeChanged();
    }

    void OnOperationModeChanged()
    {
        if (Player.Instance.m_EasyOperation)
        {
            m_RotationWidget.gameObject.SetActive(false);
            m_LockB.SetActive(true);
        }
        else
        {
            m_RotationWidget.gameObject.SetActive(true);
            m_LockB.SetActive(false);
        }
    }
    public void SetFreeCamera(bool isFree)
    {
        m_Controle.SetFreeCamera(isFree);
    }
    public void OnRotChanged(float value)
    {
        float absValue = Mathf.Abs(value);
        if (absValue < 0.01)
        {
            return;
        }
        if(value > 0)
        {
            m_Controle.ShipRotRight(absValue);
        }
        else
        {
            m_Controle.ShipRotLeft(absValue);
        }
    }
}
